package com.bwf.generic;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/24 11:02
 * @Copyright 博为峰
 */
public class GenericDemo03 {
    public static void main(String[] args) {
        final ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 20));
        ite2(students);

        final ArrayList<Worker> workers = new ArrayList<>();
        workers.add(new Worker("李四", 30));
//        ite2(workers);

        final ArrayList<String> strings = new ArrayList<>();
        strings.add("a");
//        ite2(strings);

        final ArrayList<Person> persons = new ArrayList<>();
        persons.add(new Person("李四", 30));
        ite2(persons);
    }

    //? super E 接收E类型或E类型的父类型 下限
    public static void ite2(ArrayList<? super Student> arrayList){
        final Iterator<?> iterator = arrayList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    //? extends E 接收E类型或E类型的子类型 上限
    public static void ite(ArrayList<? extends Person> arrayList){
        final Iterator<?> iterator = arrayList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
